﻿using MediatR;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.SetupCommand
{
    public class SlaughterEveryDayGroupHandler : IRequestHandler<SlaughterEveryDayGroupCommand, Result>
    {
        IBaseRepositoryGet<Slaughter> SlaughterRep;

        public SlaughterEveryDayGroupHandler(IBaseRepositoryGet<Slaughter> slaughterRep)
        {
            SlaughterRep = slaughterRep;
        }
        /// <summary>
        /// 根据选中的时间进行查询当天的所有数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(SlaughterEveryDayGroupCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Success };

            try
            {
                var list = from a in SlaughterRep.GetAll().Where(x => x.SlaughterDate == request.date)
                           select new
                           {
                               a.SlaughterNo
                           };
                           
                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = list;
                    result.Msg = "根据选中的时间进行查询当天的所有数据";
                }

            }
            catch (Exception)
            {

                throw;
            }

            return result;
        }
    }
}
